<?php
/**
 * Created by PhpStorm.
 * User: huliren<huliren0516@163.com>
 * Date: 2017/10/28
 * Time: 16:10
 */
define('SITE_AUTH', 'user-private');
require('../../../common.inc.php');

use lib\core\AdminAction;
use Circle\VirtualUser as Model;
use Common\ResourcePathManager;
use Common\Upload;
use Center\User;
use Circle\TopicLike;
use Circle\CommentLike;
use Circle\FollowCircle;
use Circle\FollowUser;

class userPrivate extends AdminAction
{
    private $list_url = 'data/modules/user/userPrivate.php';

    public function doDefault()
    {
        $smarty = $this->app->page();

        $where = ['UserId > ' => 0];
        $s_userId = intval(trim($_GET['s_userId']));
        $s_name = trim($_GET['s_name']);
        if (!empty($s_userId)) {
            $where += ['UserId' => $s_userId];
        }

        if (!empty($s_name)) {
            $where += ['NickName LIKE' => '%' . $s_name . '%'];
        }


        $page = empty($_GET['page']) ? 1 : (int)$_GET['page'];
        $limit = parent::PAGE_SIZE;
        $offset = ($page - 1) * $limit;

        $data = Model::getList($where, $offset, $limit);
        $count = $data['count'];
        if ($count > 0) {
            $page_info['page'] = ceil($count / $limit);
            $page_info['count'] = $count;

            $pagination = $this->pagination($count, $limit, $page);
            $smarty->value('page_info', $page_info);
            $smarty->value('pagination', $pagination);
            $smarty->value('list', $data['data']);
        }

        $smarty->output('user/userPrivate_list.tpl');
    }

    public function doEdit()
    {
        $smarty = $this->app->page();

        $id = $_GET['id'];
        $info = User::getDataInfoById($id);

        if (!empty($id)) {
            if (empty($info)) {
                HDshowMsg('数据不存在！', ['type' => 'error']);
            }

            $smarty->value('info', $info);
        }

        $smarty->output('user/userPrivate_edit.tpl');
    }

    public function doUpdata()
    {
        $userId = $_POST['id'];
        $nickName = $_POST['nickName'];
        $head = $_FILES['head'];

        $headUrl = '';
        if (!empty($userId)) {
            $data = [Model::FIELD_NICK_NAME => $nickName];
            if (isset($head['name']) && $head['error'] == 0 && $head['size'] > 0) {//头像入库
                $headUrl = Upload::saveFile($head['tmp_name'], ResourcePathManager::getUserHeadSavePath());
                $data = array_merge($data, ['HeadUrl' => $headUrl]);
            }

            $res = Model::updateUser($userId, $data);
        } else {
            if (isset($head['name']) && $head['error'] == 0 && $head['size'] > 0) {//头像入库
                $headUrl = Upload::saveFile($head['tmp_name'], ResourcePathManager::getUserHeadSavePath());
            }

            $res = Model::addUser($nickName, $headUrl);
        }

        if ($res) {
            HDshowMsg('操作成功', ['type' => 'success', 'url' => $this->list_url]);
        } else {
            HDshowMsg('操作失败', ['type' => 'error']);
        }
    }

    /**
     * 查询作者
     */
    public function doSelectUser()
    {
        $smarty = $this->app->page();

        $where = ['UserId > ' => 0];
        $s_userId = intval(trim($_GET['s_userId']));
        $s_name = trim($_GET['s_name']);
        if (!empty($s_userId)) {
            $where += ['UserId' => $s_userId];
        }

        if (!empty($s_name)) {
            $where += ['NickName LIKE' => '%' . $s_name . '%'];
        }

        $s_data = [
            's_userId' => $s_userId,
            's_name' => $s_name,
        ];

        $param['page'] = $page = empty($_GET['page']) ? 1 : (int)$_GET['page'];
        $limit = 20;
        $offset = ($page - 1) * $limit;

        $data = Model::getList($where, $offset, $limit, "UserId,NickName");
        $count = $data['count'];

        if ($count > 0) {
            $page_info['page'] = ceil($count / $limit);
            $page_info['count'] = $count;
            $pagination = $this->pagination($count, $limit, $page);
            $smarty->value('page_info', $page_info);
            $smarty->value('pagination', $pagination);
            $smarty->value('list', $data['data']);
        }

        $smarty->value('s_data', $s_data);
        $smarty->output('user/user_list.tpl');
    }

    /**
     * 点赞、关注跳转
     */
    public function doToLike()
    {
        $smarty = $this->app->page();
        $where = ['UserId >' => 0];

        $id = $_GET['id'];
        $type = $_GET['type'];

        $param['page'] = $page = empty($_GET['page']) ? 1 : (int)$_GET['page'];
        $limit = 20;
        $offset = ($page - 1) * $limit;

        $data = Model::getList($where, $offset, $limit, "UserId,NickName");
        $count = $data['count'];

        if ($count > 0) {
            $ids = [];
            foreach ($data['data'] as $k => $v) {
                $ids[] = $v['UserId'];
            }
            $like_data = [];
            if ($type == 1) {//帖子点赞
                $like_data = TopicLike::getList(['TopicId' => $id, 'UserId IN' => $ids], 0, 0, "UserId")['data'];
            } elseif ($type == 0) {//评论点赞
                $like_data = CommentLike::getList(['CommentId' => $id, 'UserId IN' => $ids], 0, 0, "UserId")['data'];
            } elseif ($type == 2) {//关注圈子
                $like_data = FollowCircle::getList(['CircleId' => $id, 'UserId IN' => $ids], 0, 0, "UserId")['data'];
            }elseif ($type == 3){//关注用户
                $like_data = FollowUser::getList(['ParentUserId' => $id,'UserId IN' => $ids],0,0,"UserId")['data'];
                foreach($data['data'] as $k => $v){//去除自身
                    if($v['UserId'] == $id){
                        unset($data['data'][$k]);
                    }
                }
            }

            $like_userId = [];
            foreach ($like_data as $k => $v) {
                $like_userId[] = $v['UserId'];
            }


            $page_info['page'] = ceil($count / $limit);
            $page_info['count'] = $count;
            $pagination = $this->pagination($count, $limit, $page);
            $smarty->value('page_info', $page_info);
            $smarty->value('pagination', $pagination);
            $smarty->value('list', $data['data']);
            $smarty->value('ids', implode(",",$ids));
            $smarty->value('like_userId', $like_userId);
        }

        $smarty->value('type', $type);
        $smarty->value('id', $id);
        $smarty->output('user/like_list.tpl');
    }

    /**
     * 点赞、关注
     */
    public function doChangeLike()
    {
        $type = $_POST['type'];
        $id = $_POST['id'];
        $userId = $_POST['userId'];
        //        pre($id,$type,$userId).exit();
        if ($type == 1) {
            $data = TopicLike::getList(['TopicId' => $id, 'UserId' => $userId], 0, 0, "Id")['data'];
            if (!empty($data)) {
                $res = TopicLike::deleteTopicLike($userId, $id);
            } else {
                $res = TopicLike::addTopicLike($userId, $id);
            }
        } elseif ($type == 0) {
            $data = CommentLike::getList(['CommentId' => $id, 'UserId' => $userId], 0, 0, "Id")['data'];
            if (!empty($data)) {
                $res = CommentLike::deleteCommentLike($userId, $id);
            } else {
                $res = CommentLike::addCommentLike($userId, $id);
            }
        } elseif ($type == 2) {
            $data = FollowCircle::getList(['CircleId' => $id, 'UserId' => $userId], 0, 0, "Id")['data'];
            if (!empty($data)) {
                $res = FollowCircle::remove($userId,$id);
            }else{
                $res = FollowCircle::addFollow($userId,$id);
            }
        }elseif ($type == 3){
            $data = FollowUser::getList(['ParentUserId' => $id,'UserId' => $userId],0,0,"Id")['data'];
            if(!empty($data)){
                $res = FollowUser::remove($userId,$id);
            }else{
                $res = FollowUser::addFollow($userId,$id);
            }
        }

        if ($res) {
            $this->output(['type' => 'yes']);
        } else {
            $this->output(['type' => 'no']);
        }
    }

    /**
     * 批量点赞、关注
     */
    public function doChangeAll(){
        $userIds = explode(',',$_POST['userIds']);
        $id = $_POST['id'];
        $type = $_POST['type'];

        $like_data = [];
        switch ($type){
            case 0:
                $like_data = CommentLike::getList(['CommentId' => $id, 'UserId IN' => $userIds], 0, 0, "UserId")['data'];
                break;
            case 1:
                $like_data = TopicLike::getList(['TopicId' => $id, 'UserId IN' => $userIds], 0, 0, "UserId")['data'];
                break;
            case 2:
                $like_data = FollowCircle::getList(['CircleId' => $id, 'UserId IN' => $userIds], 0, 0, "UserId")['data'];
                break;
            case 3:
                $like_data = FollowUser::getList(['ParentUserId' => $id,'UserId IN' => $userIds],0,0,"UserId")['data'];
                break;
        }

        $like_userId = [];
        foreach ($like_data as $k => $v) {
            $like_userId[] = $v['UserId'];
        }

        //没点赞的数据->差集
        $ids = array_diff($userIds,$like_userId);

        switch ($type){
            case 0:
               foreach($ids as $v){
                  $res =  CommentLike::addCommentLike($v, $id);
                   usleep(50000);
               }
                break;
            case 1:
                foreach($ids as $v){
                   $res =  TopicLike::addTopicLike($v, $id);
                    usleep(50000);
                }
                break;
            case 2:
                foreach($ids as $v){
                  $res =   FollowCircle::addFollow($v,$id);
                    usleep(50000);
                }
                break;
            case 3:
                foreach($ids as $v){
                   $res  = FollowUser::addFollow($v,$id);
                    usleep(50000);
                }
                break;
        }

        if($res){
            $this->output(['type'=>'yes']);
        }else{
            $this->output(['type'=>'no']);
        }
    }
}

$app->run();